package com.zhiyun.service.impl;

import com.zhiyun.domain.TestPaper;
import com.zhiyun.domain.TestQuestion;
import com.zhiyun.mapper.TestPaperMapper;
import com.zhiyun.mapper.TestQuestionMapper;
import com.zhiyun.service.TestPaperService;
import com.zhiyun.util.Db;
import com.zhiyun.vo.Result;

import java.util.List;

/**
 * @author zc
 * @Date 2025/4/29 下午9:12
 */
public class TestPaperServiceImpl implements TestPaperService {
    TestPaperMapper testPaperMapper = Db.getMapper(TestPaperMapper.class);
    TestQuestionMapper testQuestionMapper = Db.getMapper(TestQuestionMapper.class);

    @Override
    public Result selectAllTestPaper(int teacherId) {
        List<TestPaper> list = testPaperMapper.selectByTeacherId(teacherId);
        return Result.ok(list);
    }

    @Override
    public Result selectByIdWithQuestions(int id) {
        TestPaper testPaper = testPaperMapper.selectByIdWithQuestions(id);
        return Result.ok(testPaper);
    }

    @Override
    public Result insertTestPaper(TestPaper testPaper) {
        TestPaper oldTestPaper = testPaperMapper.selectByNameAndTeacherId(testPaper.getName(), testPaper.getTeacherId());
        if (oldTestPaper == null) {
            int rows = testPaperMapper.insert(testPaper);
            if (rows > 0) {
                return Result.okMsg("添加成功");
            }
            return Result.fail("添加失败");
        } else {
            return Result.fail("题库已经存在");
        }
    }

    @Override
    public Result updateTestPaper(TestPaper testPaper) {
        TestPaper oldTestPaper = testPaperMapper.selectByPrimaryKey(testPaper.getId());
        if (oldTestPaper == null) {
            return Result.fail("试卷不存在");
        }
        if (testPaper.getTeacherId() != oldTestPaper.getTeacherId() && (!testPaper.getName().equals(oldTestPaper.getName()))) {
            TestPaper oldTestPaper1 = testPaperMapper.selectByNameAndTeacherId(testPaper.getName(), testPaper.getTeacherId());
            if (oldTestPaper1 != null) {
                return Result.fail("该教师的题库已经存在");
            }
        }
        int rows = testPaperMapper.updateByPrimaryKey(testPaper);
        if (rows > 0) {
            return Result.okMsg("修改成功");
        }
        return Result.fail("修改失败");
    }


    @Override
    public Result deleteTestPaper(Integer id) {
        TestPaper testPaper = testPaperMapper.selectByPrimaryKey(id);
        if (testPaper == null) {
            return Result.fail("该题库不存在");
        }
        try {
            Db.startTransaction();
            int rows = testPaperMapper.deleteByPrimaryKey(id);
            //根据试卷id得到这个试卷中的所有题目
            List<TestQuestion> testQuestions = testQuestionMapper.selectByPaperIdWithQuestionType(id);
            int row = 0;
            for (TestQuestion testQuestion : testQuestions) {
                row += testQuestionMapper.deleteByPrimaryKey(testQuestion.getId());
            }
            if (!(rows == 1 && row == testQuestions.size())) {
                throw new Exception();
            }
            Db.commitTransaction();
        } catch (Exception e) {
            e.printStackTrace();
            Db.rollbackTransaction();
            return Result.fail("删除失败");
        } finally {
            Db.close();
        }
        return Result.okMsg("删除成功");
    }
}
